package ziv.map.activity;

import java.io.IOException;
import java.io.StringReader;

import com.baidu.location.LocationChangedListener;
import com.baidu.location.LocationClient;
import com.baidu.location.ReceiveListener;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import ziv.map.mapMain;
import ziv.map.bean.LocationBean;
import ziv.map.utils.Constant;
import ziv.map.utils.Location;
import ziv.utils.html.FileAndroid;
import ziv.utils.html.HttpDownloader;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class UploadLonLatService extends Service {
	String selfMobile = "";
	Handler handler = new Handler();
	// private LocationClient mLocClient;
	public LocationClient mLocationClient = null; // 定义位置客户端
	HttpDownloader httpDownloader = new HttpDownloader(); // 与后台通信

	// public mapBaiduDemo mapbaiduDemo=null;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.v("ziv", "UploadLonLatService start");
		super.onCreate();
		// 实例化位置实例
		mLocationClient = new LocationClient(this);
		/**
		 * 返回国测局经纬度坐标系 coor=gcj02 返回百度墨卡托坐标系 coor=bd09 返回百度经纬度坐标系 coor=bd09ll
		 */
		mLocationClient.setCoorType("bd09ll"); // 设置成百度坐标
		//mLocationClient.setServiceMode(arg0)
		// 添加位置改变监听
		mLocationClient
				.addLocationChangedlistener(new MyLocationChangedListener());
		// 接收定位信息
		mLocationClient.addRecerveListener(new MyReceiveListenner());
		mLocationClient.start();
	}

	@Override
	public void onStart(Intent intent, int startId) {

		super.onStart(intent, startId);
		selfMobile = Constant.selfMobile;
		// 启动循环执行
		handler.post(runnable1);

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
		mLocationClient.stop();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	Runnable runnable1 = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Log.v("ziv", "uploadLonLatService  ->  " + selfMobile);
			// mLocationClient.getLocation();
			// handler.postDelayed(runnable1, Constant.uptimeDelayed);
		}

	};

	/**
	 * 监听位置信息，当调用getLocation（） 时触发这个监听方法
	 * 
	 * @author ziv
	 * 
	 */
	public class MyLocationChangedListener implements LocationChangedListener {
		@Override
		public void onLocationChanged() {
			mLocationClient.getLocation();
			Log.v("ziv", "UploadLonLatService->onLocationChanged");
			// logMsg("LocationChangedListener: ");
		}
	}

	public class MyReceiveListenner implements ReceiveListener {
		@Override
		public void onReceive(String strData) {

			if (strData == null) {// 重新获得位置
				mLocationClient.getLocation();
				Log.v("ziv", "UploadLonLatService.MyReceiveListenner->if  "
						+ strData + "**");
			} else {// 位置入库
			// Log.v("ziv", "UploadLonLatService.MyReceiveListenner->else  **"
			// + strData + "**");

				String updLonLat = Constant.updLonLat;
				// 1.获得经纬度
				Gson gson = new Gson();
				LocationBean locationBean = gson.fromJson(strData,
						LocationBean.class);
				String x = locationBean.getContent().getPoint().getX();
				String y = locationBean.getContent().getPoint().getY();
				String lon = x.substring(0, x.indexOf(".") + 7)
						.replace(".", "");
				String lat = y.substring(0, y.indexOf(".") + 7)
						.replace(".", "");
				Log.v("ziv", "UploadLonLatService.MyReceiveListenner->lon=  **"
						+ lon + "**lat=" + lat + "***");
				Log.v("ziv", strData);
				// 2.传递给服务器
				// strData
				updLonLat = updLonLat.replace("{1}", Constant.selfMobile)
						.replace("{2}", lat).replace("{3}", lon);
				String response = httpDownloader.download(updLonLat);//
				// 添加到网络数据库

			}
			// logMsg("ReceiveListener: " + strData);
		}
	}
	// @Override
	// public void run() {
	// Intent intent=new Intent(mapbaiduDemo,UploadLonLatService.class);
	// startService(intent);
	// }

}
